//
//  HLOtherPayView.m
//  zhuanxian
//
//  Created by zhaoliang chen on 2017/11/20.
//  Copyright © 2017年 huiling. All rights reserved.
//

#import "HLOtherPayView.h"

@interface HLOtherPayView()
<UITextFieldDelegate>

@property(nonatomic,strong)HLWaybillDetailObject* waybillObject;
@property(nonatomic,strong)UIView* maskView;
@property(nonatomic,strong)UIView* valueView;
@property(nonatomic,strong)UILabel* labelTotal;
@property(nonatomic,strong)NSMutableArray* arrayTextFields;

@end

@implementation HLOtherPayView

#define rowHeight   50

- (instancetype)initWithWaybill:(HLWaybillDetailObject*)model {
    self = [self initWithFrame:CGRectZero];
    if (self) {
        self.waybillObject = [model copy];
        [self setupData];
        self.labelTotal.text = self.waybillObject.othersFees;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.arrayTextFields = [NSMutableArray new];
        
        [self addSubview:self.maskView];
        [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
        
        [self addSubview:self.valueView];
        [self.valueView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.top.mas_equalTo(self.mas_bottom);
            make.height.mas_equalTo(12*rowHeight);
        }];
        
        NSArray* titles = @[@"费用名称",@"接货费",@"垫付费",@"送货费",@"分拨费",@"代办费",@"叉吊费",@"短信费",@"包装费",@"发站仓储",@"到站仓储"];
        for (int i=0; i<11; i++) {
            UILabel* label = [[UILabel alloc]initWithFrame:CGRectZero];
            if (i==0) {
                label.textColor = [UIColor orangeColor];
                label.font = [UIFont systemFontOfSize:23];
            }
            label.textAlignment = NSTextAlignmentCenter;
            label.text = titles[i];
            [self.valueView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.width.mas_equalTo(self.valueView.mas_width).dividedBy(2.0);
                make.height.mas_equalTo(rowHeight);
                make.top.mas_equalTo(i*rowHeight);
            }];
            
            UITextField* textFieldValue = [[UITextField alloc]initWithFrame:CGRectZero];
            if (i==0) {
                textFieldValue.textColor = [UIColor orangeColor];
                textFieldValue.font = [UIFont boldSystemFontOfSize:20];
                textFieldValue.enabled = NO;
                textFieldValue.text = @"金额";
            } else {
                textFieldValue.text = @"0";
                textFieldValue.tag = i-1;
            }
            textFieldValue.textAlignment = NSTextAlignmentCenter;
            if (i==10) {
                textFieldValue.returnKeyType = UIReturnKeyDone;
            } else {
                textFieldValue.returnKeyType = UIReturnKeyNext;
            }
            textFieldValue.keyboardType = UIKeyboardTypeDecimalPad;
            textFieldValue.delegate = self;
            [self.valueView addSubview:textFieldValue];
            [textFieldValue mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.valueView.mas_centerX);
                make.width.mas_equalTo(self.valueView.mas_width).dividedBy(2.0);
                make.height.mas_equalTo(rowHeight);
                make.top.mas_equalTo(i*rowHeight);
            }];
            if (i>0) {
                [self.arrayTextFields addObject:textFieldValue];
            }
            
            UIView* bottomLine = [UIView new];
            bottomLine.backgroundColor = BackColor;
            [self.valueView addSubview:bottomLine];
            [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0.6);
                make.left.right.mas_equalTo(self.valueView);
                make.bottom.mas_equalTo(label.mas_bottom);
            }];
            
            UIView* centerLine = [UIView new];
            centerLine.backgroundColor = BackColor;
            [self.valueView addSubview:centerLine];
            [centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(0.6);
                make.top.height.mas_equalTo(label);
                make.centerX.mas_equalTo(self.valueView.mas_centerX);
            }];
        }
        
        UILabel* label1 = [UILabel new];
        label1.text = @"合计:";
        [self.valueView addSubview: label1];
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.valueView.mas_bottom).offset(-rowHeight/2.0);
            make.left.mas_equalTo(5);
        }];
        
        [self.valueView addSubview:self.labelTotal];
        [self.labelTotal mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(label1.mas_bottom);
            make.left.mas_equalTo(label1.mas_right);
        }];
        
        UIButton* btnSave = [UIButton buttonWithType:UIButtonTypeCustom];
        btnSave.backgroundColor = MainColor;
        btnSave.tag = 0;
        [btnSave setTitle:@"保存" forState:UIControlStateNormal];
        [btnSave setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnSave addTarget:self action:@selector(onButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        btnSave.layer.masksToBounds = YES;
        btnSave.layer.cornerRadius = 4;
        [self.valueView addSubview:btnSave];
        [btnSave mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(label1);
            make.size.mas_equalTo(CGSizeMake(80, 30));
            make.right.mas_equalTo(-5);
        }];
        
        UIButton* btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
        btnCancel.backgroundColor = [UIColor grayColor];
        btnCancel.tag = 1;
        [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
        [btnCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnCancel addTarget:self action:@selector(onButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        btnCancel.layer.masksToBounds = YES;
        btnCancel.layer.cornerRadius = 4;
        [self.valueView addSubview:btnCancel];
        [btnCancel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(label1);
            make.size.mas_equalTo(CGSizeMake(80, 30));
            make.right.mas_equalTo(btnSave.mas_left).offset(-10);
        }];
    }
    return self;
}

- (void)setupData {
    for (int i=0; i<self.arrayTextFields.count; i++) {
        UITextField* textFieldValue = self.arrayTextFields[i];
        switch (i) {
            case 0:
                textFieldValue.text = self.waybillObject.pickUpCargoFees;
                break;
            case 1:
                textFieldValue.text = self.waybillObject.disbursementsFees;
                break;
            case 2:
                textFieldValue.text = self.waybillObject.deliveryCargoFees;
                break;
            case 3:
                textFieldValue.text = self.waybillObject.dispatchFees;
                break;
            case 4:
                textFieldValue.text = self.waybillObject.authorizeFees;
                break;
            case 5:
                textFieldValue.text = self.waybillObject.forkLiftFees;
                break;
            case 6:
                textFieldValue.text = self.waybillObject.smsFees;
                break;
            case 7:
                textFieldValue.text = self.waybillObject.packingFees;
                break;
            case 8:
                textFieldValue.text = self.waybillObject.departStorageFees;
                break;
            case 9:
                textFieldValue.text = self.waybillObject.arriveStorageFees;
                break;
            default:
                break;
        }
    }
}

- (void)show {
    [self performSelector:@selector(delayShow) withObject:nil afterDelay:0.03];
}

- (void)onButtonAction:(UIButton*)btn {
    switch (btn.tag) {
        case 0: {
            for (int i=0; i<self.arrayTextFields.count; i++) {
                UITextField* textFieldValue = self.arrayTextFields[i];
                switch (i) {
                    case 0:
                        self.waybillObject.pickUpCargoFees = textFieldValue.text;
                        break;
                    case 1:
                        self.waybillObject.disbursementsFees = textFieldValue.text;
                        break;
                    case 2:
                        self.waybillObject.deliveryCargoFees = textFieldValue.text;
                        break;
                    case 3:
                        self.waybillObject.dispatchFees = textFieldValue.text;
                        break;
                    case 4:
                        self.waybillObject.authorizeFees = textFieldValue.text;
                        break;
                    case 5:
                        self.waybillObject.forkLiftFees = textFieldValue.text;
                        break;
                    case 6:
                        self.waybillObject.smsFees = textFieldValue.text;
                        break;
                    case 7:
                        self.waybillObject.packingFees = textFieldValue.text ;
                        break;
                    case 8:
                        self.waybillObject.departStorageFees = textFieldValue.text;
                        break;
                    case 9:
                        self.waybillObject.arriveStorageFees = textFieldValue.text;
                        break;
                    default:
                        break;
                }
            }
            self.waybillObject.othersFees = [NSString stringWithFormat:@"%.2f",([self.waybillObject.pickUpCargoFees floatValue]+[self.waybillObject.disbursementsFees floatValue]+[self.waybillObject.deliveryCargoFees floatValue]+[self.waybillObject.dispatchFees floatValue]+[self.waybillObject.authorizeFees floatValue]+[self.waybillObject.forkLiftFees floatValue]+[self.waybillObject.smsFees floatValue]+[self.waybillObject.packingFees floatValue]+[self.waybillObject.departStorageFees floatValue]+[self.waybillObject.arriveStorageFees floatValue])];
            if ([_delegate respondsToSelector:@selector(onSaveOtherPay:)]) {
                [_delegate onSaveOtherPay:self.waybillObject];
            }
        }
            break;
        case 1: {
            
        }
            break;
        default:
            break;
    }
    [self onRemoveMe];
}

- (void)delayShow {
    [self.valueView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(12*rowHeight);
    }];
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    CGFloat total = 0.0;
    for (int i=0; i<self.arrayTextFields.count; i++) {
        UITextField* textFieldValue = self.arrayTextFields[i];
        total += [textFieldValue.text floatValue];
    }
    self.labelTotal.text = [NSString stringWithFormat:@"%.2f",total];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag < self.arrayTextFields.count-1) {
        UITextField* t = self.arrayTextFields[textField.tag+1];
        [t becomeFirstResponder];
    } else {
        [textField resignFirstResponder];
    }
    return YES;
}

- (void)onRemoveMe {
    [self.valueView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(self.mas_bottom);
        make.height.mas_equalTo(12*rowHeight);
    }];
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (self) {
            [self removeFromSuperview];
        }
    }];
}

- (UIView*)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc]init];
        _maskView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onRemoveMe)];
        [_maskView addGestureRecognizer:tap];
    }
    return _maskView;
}

- (UIView*)valueView {
    if (!_valueView) {
        _valueView = [[UIView alloc]init];
        _valueView.backgroundColor = [UIColor whiteColor];
        self.valueView.clipsToBounds = YES;
    }
    return _valueView;
}

- (UILabel*)labelTotal {
    if (!_labelTotal) {
        _labelTotal = [UILabel new];
        _labelTotal.text = @"0";
        _labelTotal.textColor = [UIColor redColor];
        _labelTotal.font = [UIFont systemFontOfSize:19];
    }
    return _labelTotal;
}

@end
